	
	**This .do file produces detainers results**
	
		

			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
	
*-------------------------------------------------------------------------------
* Start with Counts data
*-------------------------------------------------------------------------------

use "detainerremovalcounts", clear


*how many detainers total

*logs
foreach var of varlist detainercount-declinecount{
gen log`var' = ln(`var' + 1)
}


*how many detainers total during this relevant period?
tabstat detainercount, stat(sum)

**
*Make indicator variables
**


gen centeredmonth = month-policymonth

replace centeredmonth = 0 if policymonth == .

*Indicator variables for each centered month variable (only 5 years before and after)
tab centeredmonth if centeredmonth >= -10 & centeredmonth <= 10, gen(indcentered)

* Rename these 
forv j = 1/10{
	loc k = abs(`j' - 11)
	rename indcentered`j' indcenteredN`k'
}
forv j = 11/21{
	loc k = `j' - 11
	rename indcentered`j' indcentered`k'
}

*Now set first indicator to include all previous obs and last to include all subsequent
replace indcenteredN10 = 1 if centeredmonth <= -10
replace indcentered10 = 1 if centeredmonth >= 10 

* Change order of indcenteredN0 (for which y - y* = 0) so that is omitted category
order indcentered0, last

*Replace missing months with zeros 
foreach var of varlist ind* {
replace `var' = 0 if `var' != 1
}



***
*Run regressions and produce figures
***

**
* All counts
**

glm detainercount  i.statecounty i.month indcenteredN10-indcentered10 if month >= firstmonth, family(nbinomial ml) ///
cluster(state) link(log) irls

preserve

* Get coef
regsave indcenteredN10-indcentered10, detail(all)

* Create variables
gen ub = coef + 1.96   * stderr  * sqrt(deviance/df)
gen lb = coef - 1.96 * stderr  * sqrt(deviance/df)


* Generate time variable and other graphing stuff
gen time = _n-11
replace time = time+1 if time > -1


twoway (rspike ub lb time , lw(0.3) lc(gray)) (scatter coef time , msize(small) msymbol(circle) ), ///
	yli(0, lpattern(dash) lc(black)) ///
	ylab(-2(.5)1, labsize(small) angle(horizontal)) xlab(-10(2)10, labsize(small)) ///
	title("Detainers Issued") ///
	ytitle("") xtitle("") ///
	graphregion( fcolor(white) lcolor(white)) plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none)) ///
	legend(off) xsize(5) ysize(3) name(a, replace)


restore

**
* Book in counts
**

glm bookincount indcenteredN10-indcentered10 i.month i.statecounty if month >= firstmonth , family(nbinomial ml) ///
cluster(state) link(log) irls

preserve

* Get coef
regsave indcenteredN10-indcentered10, detail(all)

* Create variables
gen ub = coef + 1.96   * stderr  * sqrt(deviance/df)
gen lb = coef - 1.96 * stderr  * sqrt(deviance/df)

* Generate time variable and other graphing stuff
gen time = _n-11
replace time = time+1 if time > -1

twoway (rspike ub lb time , lw(0.3) lc(gray)) (scatter coef time , msize(small) msymbol(circle) ), ///
	yli(0, lpattern(dash) lc(black)) ///
	ylab(-2(.5)1, labsize(small) angle(horizontal)) xlab(-10(2)10, labsize(small)) ///
	title("Detainer-Based Arrests") ///
	ytitle("") xtitle("") ///
	graphregion( fcolor(white) lcolor(white)) plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none)) ///
	legend(off) xsize(5) ysize(3) name(b, replace)


restore


graph combine a b, xsize(3) ysize(3.5) ///
l1title("Change in Log Counts" "Relative to Date of Policy", size(small)) ///
b1title("Months Since Sanctuary Policy") ti("") rows(2)

graph export "fig3.pdf", replace










